package cn.com.yx.ms.controller;

import cn.com.yx.ms.domain.dbmodel.GgMain;
import cn.com.yx.ms.domain.dbmodel.ModelControl;
import cn.com.yx.ms.domain.dbmodel.ModelMain;
import cn.com.yx.ms.domain.model.ModelControlModel;
import cn.com.yx.ms.domain.model.ModelMainModel;
import cn.com.yx.ms.domain.model.SimpleModel;
import cn.com.yx.ms.domain.mq.MessageBase;
import cn.com.yx.ms.domain.mq.upstream.U_Message4009;
import cn.com.yx.ms.service.IGgService;
import cn.com.yx.ms.service.ILicenseService;
import cn.com.yx.ms.service.IModelService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.view.ApiResult;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 栏目/模板管理
 */
@RestController
@RequestMapping(value = "/")
@Api(value = "栏目/模板管理接口", description = "栏目/模板管理相关信息")
public class ModelController {

    @Resource
    private IModelService iModelService;

    @Resource
    private IGgService iGgService;

    @Resource
    private ILicenseService iLicenseService;

    @ResponseBody
    @RequestMapping(value = "model/getAllModelControl", method = RequestMethod.POST)
    @ApiOperation("获取/查询栏目一览")
    public ApiResult getAllModelControl(@RequestBody @Validated ModelControlModel modelModel) {
        if (modelModel.getPage_pi() == 0 || modelModel.getPage_ps() == 0) {
            return new ApiResult<>(AppConstant.MSG_FAILURE, false, "页码信息错误", "");
        }
        return iModelService.getAllModelControl(modelModel);
    }

    @ResponseBody
    @RequestMapping(value = "model/addModelControl", method = RequestMethod.POST)
    @ApiOperation("新增栏目")
    public ApiResult addModelControl(@RequestBody @Validated ModelControl modelControl) {
        return iModelService.addModelControl(modelControl);
    }

    @ResponseBody
    @RequestMapping(value = "model/editModelControl", method = RequestMethod.POST)
    @ApiOperation("修改栏目")
    public ApiResult editModelControl(@RequestBody @Validated ModelControl modelControl) {
        return iModelService.editModelControl(modelControl);
    }

    @ResponseBody
    @RequestMapping(value = "model/disableModelControl/{guid}", method = RequestMethod.GET)
    @ApiOperation("停用栏目")
    public ApiResult disableModelControl(@PathVariable("guid") String guid) {
        return iModelService.disableModelControl(guid);
    }

    @ResponseBody
    @RequestMapping(value = "model/getAllModelMain", method = RequestMethod.POST)
    @ApiOperation("获取/查询模板一览分页")
    public ApiResult getAllModelMain(@RequestBody @Validated ModelMainModel modelModel) {
        //System.out.println("进入查询模板");
        //System.out.println("查询的状态"+modelModel.getIsuse());

        return iModelService.getAllModelMain(modelModel);
    }

    @ResponseBody
    @RequestMapping(value = "model/addModelMain", method = RequestMethod.POST)
    @ApiOperation("新增模板")
    public ApiResult addModelMain(@RequestBody @Validated ModelMainModel modelMain) {
        String no = iLicenseService.getMaxNo("model_main", "no", "M");
        modelMain.setNo(no);
        return iModelService.addModelMain(modelMain);
    }

    @ResponseBody
    @RequestMapping(value = "model/editModelMain", method = RequestMethod.POST)
    @ApiOperation("修改模板")
    public ApiResult editModelMain(@RequestBody @Validated ModelMainModel modelMain) {
        return iModelService.editModelMain(modelMain);
    }

    @ResponseBody
    @RequestMapping(value = "model/getModelControl", method = RequestMethod.GET)
    @ApiOperation("获取栏目下拉框")
    public ApiResult getModelControl() {
        return iModelService.getModelControl();
    }

    @ResponseBody
    @RequestMapping(value = "model/disableModelMain/{guid}", method = RequestMethod.GET)
    @ApiOperation("停用模板")
    public ApiResult disableModelMain(@PathVariable("guid") String guid) {
        return iModelService.disableModelMain(guid);
    }

    @ResponseBody
    @RequestMapping(value = "model/selectModel/{guid}", method = RequestMethod.GET)
    @ApiOperation("查找模板的关联数")
    public ApiResult selectModel(@PathVariable("guid") String guid) {
        return iModelService.countModel(guid);
    }

//    @ResponseBody
//    @RequestMapping(value = "model/export", method = RequestMethod.POST)
//    @ApiOperation("导出模板")
//    public ApiResult exportModel(@RequestBody @Validated ModelMain modelMain, HttpServletResponse res){
//        return iModelService.exportModel(modelMain.getGuid(),res);
//    }

    @ResponseBody
    @RequestMapping(value = "model/import", method = RequestMethod.POST)
    @ApiOperation("导入模板")
    public ApiResult importModel(HttpServletRequest request) {
        return iModelService.importModel(request);
    }

    @ResponseBody
    @RequestMapping(value = "model/getModelMainList", method = RequestMethod.POST)
    @ApiOperation("获取/查询模板一览")
    public ApiResult getModelMainList() {
        return iModelService.getModelMainList();
    }

    @ResponseBody
    @RequestMapping(value = "model/{styleid}/getModelMainList", method = RequestMethod.GET)
    @ApiOperation("获取/查询模板一览")
    public ApiResult getModelMainListByStyleId(@PathVariable("styleid") String styleid) {
        return iModelService.getModelMainListByStyleId(styleid);
    }

    @ResponseBody
    @RequestMapping(value = "model/{styleid}/devs", method = RequestMethod.GET)
    @ApiOperation("获取/查询模板一览")
    public ApiResult getDevsByStyleId(@PathVariable("styleid") String styleid) {
        return new ApiResult().success(iModelService.getDevsByStyleId(styleid));
    }

    @ResponseBody
    @RequestMapping(value = "model/{styleid}/equipandsites", method = RequestMethod.GET)
    @ApiOperation("获取式样下关联的设备")
    public ApiResult getEquipmentAndSiteByStyleId(@PathVariable("styleid") String styleid) {
        return new ApiResult().success(iModelService.getEquipmentAndSiteByStyleId(styleid));
    }

    @ResponseBody
    @RequestMapping(value = "model/deleteModelMain/{guid}", method = RequestMethod.GET)
    @ApiOperation("删除模板")
    public ApiResult deleteModelMain(@PathVariable("guid") String guid) {
        return iModelService.deleteModelMain(guid);
    }

    @ResponseBody
    @RequestMapping(value = "model/editDefaultModel/{guid}", method = RequestMethod.GET)
    @ApiOperation("修改默认式样的模板")
    public ApiResult editDefaultModel(@PathVariable("guid") String guid) {
        System.out.println("guid" + guid);
        return iModelService.editDefaultModel(guid, "默认式样");
    }

    @RequestMapping(value = "models", method = RequestMethod.GET)
    public List<SimpleModel> findModels() {
        return iModelService.findModels();
    }

    @ResponseBody
    @RequestMapping(value = "model/{equipmentno}/gg", method = RequestMethod.GET)
    @ApiOperation("获取设备公告信息")
    public List<String> findDevGg(@PathVariable("equipmentno") String equipmentno) throws Exception {
        List<GgMain> ggMains = iGgService.findGgByEquipmentNo(equipmentno);
        List<String> ggs = new ArrayList<>();
        if (ggMains != null) {
            for (int i = 0; i < ggMains.size(); i++) {
                ggs.add(ggMains.get(i).getContent());
            }
        }
        return ggs;
    }
}
